﻿Imports System.Web.UI.WebControls
Imports System.Web.UI
Imports DBase.scafolding.Conf
Imports DBase.MySql

Namespace DBase.scafolding

    Public Class CustomControlSearch
        Inherits WebControl
        Implements INamingContainer

        Public Event Seleziona()
        Dim WithEvents TxtFieldID As New TextBox
        Dim WithEvents BtnSearch As New Button
        Dim WithEvents TxtFieldDescription As New TextBox
        Dim WithEvents Grid As New DBase.scafolding.CustomControlGrid
        Dim WithEvents pnlGrid As New Panel

        Public Sub New()
            Me.EnsureChildControls()
            Me.InitControl()
        End Sub

        Public Property Value() As String
            Get
                Return Me.TxtFieldID.Text
            End Get
            Set(value As String)
                Me.TxtFieldID.Text = value
                Dim q = Me.field.ConfigTable.SqlView.Clone
                If value <> "" Then
                    _Description = q.Where(field.SqlFieldID, "=", value).SetSelect(field.SqlFieldDescription).ToObj
                End If
                Me.TxtFieldDescription.Text = _Description
            End Set
        End Property

        Private _Description As String
        Public ReadOnly Property Description() As String
            Get
                Return _Description
            End Get
        End Property

        Private _field As Field
        Public Property field() As Field
            Get
                Return _field
            End Get
            Set(ByVal value As Field)
                _field = value
                SetConfig()
            End Set
        End Property

        Private Sub InitControl()

            TxtFieldID.Enabled = False
            BtnSearch.Text = "Cerca"
            TxtFieldDescription.ID = "TxtFieldDescription"
            TxtFieldDescription.Enabled = False
            Grid.CssClass = "table_result_light_search"
            pnlGrid.CssClass = "Panel_Search_Grid"
            Grid.Visible = False

        End Sub

        Private Sub SetConfig()

            'Me.field.ConfigTable.Fields = DBase.scafolding.Fields.GetFieldsSql(Me.field.ConfigTable.DB, Me.field.ConfigTable.SqlView)
            Me.Grid.Config = Me.field.ConfigTable
            Me.pnlGrid.Controls.Add(Grid)

        End Sub

        Private Sub CustomControlSearch_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            If Not Me.Page.IsPostBack Then
                Me.Grid.Visible = False
            End If
        End Sub

        Protected Overrides Sub CreateChildControls()
            Controls.Clear()
            Controls.Add(TxtFieldID)
            Controls.Add(BtnSearch)
            Controls.Add(TxtFieldDescription)
            Controls.Add(New LiteralControl("<br />"))
            Controls.Add(pnlGrid)
        End Sub

        Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
            For Each c As Object In Controls
                c.RenderControl(output)
            Next
        End Sub

        Protected Sub BtnSearch_Click(sender As Object, e As System.EventArgs) Handles BtnSearch.Click
            Me.Grid.Visible = Not Me.Grid.Visible
            Me.Grid.Bind()
        End Sub

        Protected Sub usercontrol_Seleziona(id As System.Collections.Generic.Dictionary(Of String, Object)) Handles Grid.EventSelectInLine
            Dim DT As DataTable

            Dim q = Me.field.ConfigTable.SqlView.Clone
            TxtFieldID.Text = ""
            For Each pks In id
                TxtFieldID.Text &= pks.Value
                DT = q.Where(field.SqlFieldID, "=", pks.Value).ToDataTable
                Exit For
            Next

            Dim dr As DataRow
            dr = DBase.scafolding.Fields.GetDataRow(Me.field.ConfigTable.DB, id, Me.field.ConfigTable.SqlView)
            TxtFieldDescription.Text = dr(Me.field.SqlFieldDescription)
            Me.Grid.Visible = False
            RaiseEvent Seleziona()
        End Sub

    End Class
End Namespace
